//城市联动
var tpl = $("#myTemplate").html();
var template = Handlebars.compile(tpl);
function city() {
    var provinceId = $("#province").find("option:selected").val();
    $.ajax({
     url:"/sbi/city.json",
     type:"get",
      dataType:"json",
        data: {
          provinceId: provinceId
        },
     success:function(data){
          var html = template(data);
          $('#city').empty();
          $('#city').append(html);
      },
      error: function(msg) {
          console.log(msg)
      }
  })
}
   city();
$('#province').change(function (e) {
    city();
 })

//上传图片到七牛
function uploadFile(obj, type){
        var file = obj.files[0];
        var name = file.name;
        var imgtype = file.name.split('.')[1];
        var reader = new FileReader();
        reader.onload = function(e){
        var imgBase64Data = e.target.result;
        var pos = imgBase64Data.indexOf("4")+2;
        imgBase64Data = imgBase64Data.substring(pos, imgBase64Data.length - pos);
        console.log(imgBase64Data)
       $.ajax({
           url:"/qiniu/upToken.json",
           type:"get",
            dataType:"json",
           success:function(data){
                var uptoken = data.data.uptoken;
               var qiniuUploadUrl;
               if (window.location.protocol === 'https:') {
                   qiniuUploadUrl = 'https://up.qbox.me';
               } else {
                   qiniuUploadUrl = 'http://upload.qiniu.com';
               }
                    $.ajaxFileUpload({
                        url:qiniuUploadUrl,
                      secureuri:false,
                      fileElementId: "fileUpload"+type,
                      dataType:'json',
                        type:'post',
                        timeout:1000,
                        contentType:"multipart/form-data",
                      data:{
                            'type':type,
                            'token':uptoken,
                            'chunk':'0',
                            'chunks':'1',
                            'key':md5(imgBase64Data)+'.'+imgtype,
                            'file':name
                        },
                        success:function(data){
                        console.log($('.user-portrait').attr('id') +"/"+ data.key)
                            $('.user-portrait').attr('src',$('.user-portrait').attr('id') +"/"+ data.key);
                               seajs.setAppData("portrait", $('.user-portrait').attr('id') +"/"+ data.key);
                               $('.portrait img').attr('src',$('.user-portrait').attr('id') +"/"+ data.key);
                        },
                        error:function(data){
                            console.log("服务器异常");

                        }
                    });
                },
                error: function(msg) {
                console.log(msg)
                }
            })  
    }
    reader.readAsDataURL(file);
    return false;  
}

$('.email').blur(function(){
var email = $('.email').val();
var regEmail = new RegExp(/^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/);
    if(!email.match(regEmail)){
        $('.err-info').html('请输入您的邮箱');
    }else{
        $('.err-info').html('');
    }
})
$('.info-btn').click(function(){
var regMobile =  new RegExp(/^((\d{11})|^((\d{7,8})|(\d{4}|\d{3})-(\d{7,8})|(\d{4}|\d{3})-(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1})|(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1}))$)$/);
var regEmail = new RegExp(/^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/);
var portrait = $('.user-portrait').attr('src');
var province = $("#province option:selected").val();
var city = $("#city option:selected").val();
var sex = Number($('input:radio[name="sex"]:checked').val());
var workAddress = $('.work').val();
var profession = $('.profession').val();
var mobile = $('.row-mobile').val();
var qq = $('.qq').val();
var email = $('.email').val();
var address = $('.address').val();
var introduction = $('.introduction').val();
    if(!email.match(regEmail)){
        $('.err-info').html('请输入正确格式邮箱');
        return false;
    }
    var data = {
        portrait:portrait,
        provinceId:province,
        cityId:city,
        gender:sex,
        workAddress:workAddress,
        profession:profession,
        mobile:mobile,
        qq:qq,
        email:email,
        address:address,
        introduction:introduction
    }
    $.ajax({
       url:"/user.json",
       type:"patch",
          data:data,
        dataType:"json",
       success:function(data){
            console.log(data);
            $('.err-info').html('修改成功');
        },
        error: function(err) {
            $('.err-info').html(JSON.parse(err.responseText).errors[0].msg);
        }
    })
});